package leetcode.array;

/**
 * @Description: https://leetcode.cn/problems/search-rotate-array-lcci/
 * @Author Ammar
 * @Create 2023/9/13 18:58
 */
public class _面试题_10_03_搜索旋转数组 {


    public int search(int[] nums, int target) {
        if (nums[0] == target) return 0;
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (nums[mid] == target) {
                while (mid > 1 && nums[mid - 1] == target) mid--;
                return mid;
            }
            else if (nums[mid] > nums[left]) {
                if (target >= nums[left] && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else if (nums[mid] < nums[left]) {
                if (target <= nums[right] && target > nums[mid]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            } else {
                left++;
            }
        }
        return -1;
    }
}
